package com.example.springbootdemo.utils;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Base64;

/**
 * @ClassName: FileUtils
 * @Description:
 * @Author: TXW
 * @Date: 2023-07-14
 */

public class FileUtils {

    public static String fileToBase64(File file) throws IOException {
        // 读取文件内容到字节数组
        byte[] fileBytes = new byte[(int) file.length()];
        try (FileInputStream fis = new FileInputStream(file)) {
            fis.read(fileBytes);
        }

        // 将字节数组编码为 Base64 字符串
        String base64String = Base64.getEncoder().encodeToString(fileBytes);
        return base64String;
    }
}
